package com.wang.web3;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.wang.web3.adapter.MarketGrilAdapter;
import com.wang.web3.bean.MarketGirlBean;
import com.wang.web3.data.service.Business;
import com.wang.web3.data.service.ICallBackListener;

import java.util.ArrayList;
import java.util.List;


/**
 * 少女市场界面
 */
public class MarketGirlFragment extends Fragment {


    private RecyclerView mRv;
    private List<MarketGirlBean.DataBean> mGirlListArrayList;
    private BaseQuickAdapter mGirlAdapter;
    private MarketGirlBean girlBean;

    public static MarketGirlFragment newInstance() {
        MarketGirlFragment fragment = new MarketGirlFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_market_girl, container, false);
        mRv = view.findViewById(R.id.mRv);
        init();
        return view;
    }


    MarketGrilAdapter.PersonHolderListener personHolderListener = new MarketGrilAdapter.PersonHolderListener() {
        @Override
        public void onPersonItemClicked(View v, MotionEvent event, int position) {
            Intent i = new Intent(getContext(), GirlsDetailActivity.class);
            i.putExtra("girlId", girlBean.getData().get(position).getGirlId());
            startActivity(i);
        }
    };

    private void init() {
        mGirlListArrayList = new ArrayList<>();
        mGirlAdapter = new MarketGrilAdapter(mGirlListArrayList, personHolderListener);
        //设置布局管理
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        mRv.setLayoutManager(layoutManager);
//        mRv.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
        mRv.setAdapter(mGirlAdapter);
        doGirlsTask("girl", 1, 20);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
//            getKittiesCount();
        }
    }


    public void doGirlsTask(String currentTabs, int pageNum, int pageSize) {
        Business.getInstance().doGirlsTask(currentTabs, pageNum, pageSize, new ICallBackListener() {
            @Override
            public void onSuccess(String mCallBackVo) {
                Gson mGson = new Gson();
                girlBean = mGson.fromJson(mCallBackVo, MarketGirlBean.class);
                mGirlListArrayList.addAll(girlBean.getData());
                mGirlAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFaild(String mCallBackVo) {

            }
        });
    }

//    //获取该账号下的少女
//    private void getKittiesCount() {
//        try {
//
//            BigInteger bigInteger1 = kittyCore.totalSupply().sendAsync().get();
//            for (int i = currentI; i < bigInteger1.intValue(); i++) {
//                currentI = currentI + 1;
//                Tuple5<String, BigInteger, BigInteger, BigInteger, BigInteger> tuple5 = saleClockAuction.getAuction(new BigInteger((i + 1) + "")).sendAsync().get();
//                if (tuple5 != null) {
//                    Tuple4<BigInteger, BigInteger, BigInteger, BigInteger> tuple4 = randomBox.kittyIndexToDemoEquipement(new BigInteger((i + 1) + "")).sendAsync().get();
//                    list.add(new KittyInfoBean(i + 1, tuple4));
//                    Log.e("wzz", "---" + i);
//                }
//            }
//            kittyGrilAdapter.setNewData(list);
//
//            Log.e("wzz", "---" + list.size());
//
//        } catch (InterruptedException e) {
//
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//            getKittiesCount();
//        }
//
//    }

    //    //加密猫信息
//    public void getKittyInfo(String kittyId, boolean isUpdata) throws ExecutionException, InterruptedException {
//
//        BigInteger bigInteger = new BigInteger(kittyId);
//        Tuple11<Boolean, Boolean, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger> tuple11 = kittyCore.getKitty(bigInteger).sendAsync().get();
//        String info1 = "isGestating:" + tuple11.getValue1() +
//                "\n isReady:" + tuple11.getValue2() +
//                "\n cooldownIndex:" + tuple11.getValue3() +
//                "\n nextActionAt:" + tuple11.getValue4() +
//                "\n siringWithId:" + tuple11.getValue5() +
//                "\n birthTime:" + CalendarUtils.getStartStringTime(tuple11.getValue6().longValue(), "yyyy-MM-dd");
//
//        String info2 = "matronId:" + tuple11.getValue7() +
//                "\n sireId:" + tuple11.getValue8() +
//                "\n generation:" + tuple11.getValue9() +
//                "\n genes:" + tuple11.getValue10() +
//                "\n level:" + tuple11.getValue11();
//
//       /* uint8 wingType;
//        uint8 photoFrameType;
//        uint256 wingTime;
//        uint256 photoFrameTime;*/
//        Tuple4<BigInteger, BigInteger, BigInteger, BigInteger> tuple4 = randomBox.kittyIndexToDemoEquipement(bigInteger).sendAsync().get();
//        String info3 = "wingType:" + tuple4.getValue1() +
//                "\n wingTime:" + CalendarUtils.getStartStringTime(tuple4.getValue3().longValue(), "yyyy-MM-dd hh:mm:ss") +
//                "\n photoFrameType:" + tuple4.getValue2() +
//                "\n photoFrameTime:" + CalendarUtils.getStartStringTime(tuple4.getValue4().longValue(), "yyyy-MM-dd hh:mm:ss");
//
//        boolean wings = false;
//        boolean photo = false;
//        if (tuple4.getValue1().intValue() == 1) {
//            wings = true;
//        }
//        if (tuple4.getValue2().intValue() == 1) {
//            photo = true;
//        }
//
//        info3 = info3 + "market";
//        if (isUpdata) {
//            kittyWindow.upDateInfo(info1, info2, info3);
//            kittyWindow.haveProps(wings, photo);
//        } else {
//            kittyWindow = KittyWindow.showKitty(getChildFragmentManager(), info1, info2, info3);
//            kittyWindow.initOnClick(this);
//        }
//
//    }
//
//    @Override
//    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.mBtnBuyKitty:
////                buyKitty();
//                break;
//            case R.id.mBtnCancleKitty:
////                CancelKitty();
//                break;
//        }
//    }
//
//    private void CancelKitty() {
//        BigInteger bigInteger = new BigInteger("" + kittyCurrentId);
//        try {
//            TransactionReceipt receipt = saleClockAuction.cancelAuction(bigInteger).sendAsync().get();
//            List<SaleClockAuction.AuctionCancelledEventResponse> list = saleClockAuction.getAuctionCancelledEvents(receipt);
//            if (receipt.getStatus().equals("1")) {
//                Toast.makeText(getActivity(), "OK", Toast.LENGTH_SHORT).show();
//                getKittiesCount();
//            }
//            Log.e("Tag", "--");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//    }
//
//    private void buyKitty() {
//
//        BigInteger bigInteger = new BigInteger("" + kittyCurrentId);
//        try {
//            /*            auction.seller,
//            auction.startingPrice,
//            auction.endingPrice,
//            auction.duration,
//            auction.startedAt*/
//            Tuple5<String, BigInteger, BigInteger, BigInteger, BigInteger> tuple5 = saleClockAuction.getAuction(bigInteger).sendAsync().get();
//            Log.e("Tag", "===" + tuple5.getValue1());
////            (当前时间-开始时间) *（(结束价格 - 开始价格) / 持续时间） + 开始价格
//            long bigInteger1 = (int) (CalendarUtils.getCurrentTimes() / 1000 - tuple5.getValue5().longValue()) * (tuple5.getValue3().subtract(tuple5.getValue2()).divide(tuple5.getValue4()).longValue()) + tuple5.getValue2().intValue();
//            TransactionReceipt receipt = saleClockAuction.bid(bigInteger, new BigInteger(bigInteger1 + "")).sendAsync().get();
//            List<SaleClockAuction.AuctionSuccessfulEventResponse> list = saleClockAuction.getAuctionSuccessfulEvents(receipt);
//            if (receipt.getStatus().equals("1")) {
//                Toast.makeText(getActivity(), "购买成功", Toast.LENGTH_SHORT).show();
//            }
//            Log.e("Tag", "---");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//            Toast.makeText(getActivity(), "--" + e.getMessage(), Toast.LENGTH_SHORT).show();
//        }
//
//    }
}
